/*
 * @Author: liu wang wang
 * @Date: 2020-07-07 19:35:59
 * @LastEditTime: 2020-08-14 17:21:07
 * @LastEditors: liu wang wang
 * @Description: 接口请求类
 * @FilePath: \st-sdk-js\src\api\index.ts
 */
import Request from './base';
import { requestOptions, processAjaxInterface, RequestConfig } from '../../types'
import store from '../utils/store';
import msg from '../utils/Msg';
class Api extends Request {
    constructor() {
        super()
    }
    protected processAjax(options: RequestConfig): void {
        let lastSuccess = options.success;
        this.ajax({
            ...options,
            success:  (res: any) => {
                if (res.code === 200 || res.code===401) {
                    lastSuccess && lastSuccess(res);
                    return;
                }
                if (res.code === 402) {
                    this.refreshSign(options);
                    return;
                }
                msg.msg(res.message);
            }
        });
    }
    protected refreshSign(options: processAjaxInterface): void {
        const url = store.getStore('signUrl');
        this.getSign(url, {
            success:  (res: any)=> {
                if (!res || (typeof res === 'string' && res.length >= 200)) {
                    throw new Error('签名获取失败');
                } else {
                    store.setStore('sign', res);
                }
                this.processAjax(options);
            }
        });
    }
    // 获取版本库类型
    public getTypeList(options: requestOptions): void {
        this.processAjax({
            url: '/Repository/GetTypeList',
            ...options
        });
    }
    // 获取版本库详情
    public getDetail(options: requestOptions): void {
        this.processAjax({
            url: '/Repository/GetDetails',
            ...options,
        });
    }
    // 版本列表
    public getList(options: requestOptions): void {
        this.processAjax({
            url: '/Repository/GetList',
            ...options,
        });
    }
    // 绑定工具服到版本库
    public bind(options: requestOptions): void {
        this.processAjax({
            url: '/Repository/Bind',
            method: 'post',
            ...options,
        });
    }
    // 添加版本库
    public add(options: requestOptions): void {
        this.processAjax({
            url: '/Repository/Add',
            method: 'post',
            ...options,
        });
    }
    // 复制工具服
    public copy(options: requestOptions): void {
        this.processAjax({
            url: '/Repository/Copy',
            method: 'post',
            ...options
        })
    }
    // 修改版本信息
    public modify(options: requestOptions): void {
        this.processAjax({
            url: '/Repository/Modify',
            method: 'post',
            ...options
        });
    }
    // 请求签名
    public getSign(url: string, options: requestOptions): void {
        this.ajax({
            url: url,
            ...options
        })
    }
    // 判断工具服、测试区是否有版本库
    public checkHasVersion(options: requestOptions): void {
        this.processAjax({
            url: '/Repository/CheckHasVersion',
            ...options
        });
    }
}
const api = new Api();
export default api;